<?php
namespace Home\Controller;

use Component\Home2Controller;

class HomeController extends Home2Controller
{
    public function home()
    {
        $this->display();
    }

    //左边栏
    function homeleft(){
        $sql = "select * from sw_userauth";
        $sinfo = D()->query($sql);
        $this -> assign('sinfo',$sinfo);
        $this -> display();
    }


    //右部内容
    function homeright()
    {
        //获取分享
        $mymessage = D("Message");
        //1. 获得当前记录总条数
        $total = $mymessage-> where("username='".$_SESSION["username"]."'")->count();
        $per = 10;//一页显示几行
        //2. 实例化分页类对象
        $page = new \Component\Page($total, $per); //autoload
        //3. 拼装sql语句获得每页信息
        $sql = "select * from sw_message " ;
        $sql=$sql."where username='".$_SESSION["username"]."' order by message_time desc ";
        $sql=$sql. $page->limit;
        //echo $sql;
        $info = $mymessage->query($sql);
        //4. 获得页码列表
        $pagelist = $page->fpage();

        //获取评论条数，循环遍历插入info数组
        $comment=D("Comment");
        foreach($info as $k => $v){
            $c=$comment->where("message_id='".$v["message_id"]."'")->count();
            $info[$k]["comment_count"]=$c;
        }

        $this->assign('info', $info);
        $this->assign('pagelist', $pagelist);
        $this->display();
        }

    //显示登陆用户信息
    function information()
    {
        $user = D("User");
        if (!empty($_POST)) {
            $user->create();
            $rst = $user->save();
            if ($rst) {
                $this-redirect("information");
            }else {
                $this->assign("error","新旧信息一致，无修改");
                $info = $user->find($_SESSION["user_id"]); //一维数组
                $this->assign('info', $info);
                $this->display();
            }
        } else {
            $info = $user->find($_SESSION["user_id"]); //一维数组
            $this->assign('info', $info);
            $this->display();
        }
    }

    //修改密码
    public function chpwd(){
        $user = new \Model\UserModel();
        if (!empty($_POST)) {
            $rst = $user -> checkNamePwd($_SESSION['username'],$_POST['password3']);
            if($rst === false){
                $this->assign("pwd_error","旧密码错误");
            } else {
                if(trim($_POST['password'])!="") {
                    $user->create();
                    $rst = $user->save();
                    if ($rst) {
                        $this->assign("pwd_error", "修改成功");
                    } else {
                        $this->assign("pwd_error", "新旧密码一致，无修改");
                    }
                }else{
                    $this->assign("pwd_error", "新密码为空，修改失败");
                }
            }
        }
        $this->display();
    }

    //我关注的用户
    function my_followlist(){
        $follow=D("Follow");
        $info=$follow->where("follower_id=".$_SESSION["user_id"])->select();

        $total = $follow->where("follower_id=".$_SESSION["user_id"])->count();
        $per = 10;//一页显示几行
        //2. 实例化分页类对象
        $page = new \Component\Page($total, $per); //autoload
        //3. 拼装sql语句获得每页信息
        $sql = "select * from sw_follow " ;
        $sql=$sql."where follower_id='".$_SESSION["user_id"]."' ";
        $sql=$sql. $page->limit;
        //echo $sql;
        $info = $follow->query($sql);
        //4. 获得页码列表
        $pagelist = $page->fpage();

        $this->assign('pagelist', $pagelist);//跳转导航列表


        if(!empty($info)){
            $str='';
            foreach($info as $v){
                $str=$str.$v["user_id"].",";
            }
            $str = rtrim($str,',');

            $user=D("User");
            $uinfo=$user->where("user_id in (".$str.")")->select();
            $this->assign("uinfo",$uinfo);
        }else{
            $this->assign("follow_info","你还没有关注任何人");
        }
        $this->display();

    }

    //我的粉丝
    function  my_followerlist(){
        $follow=D("Follow");

        $total = $follow-> where("user_id=".$_SESSION["user_id"])->count();
        $per = 10;//一页显示几行
        //2. 实例化分页类对象
        $page = new \Component\Page($total, $per); //autoload
        //3. 拼装sql语句获得每页信息
        $sql = "select * from sw_follow " ;
        $sql=$sql."where user_id='".$_SESSION["user_id"]."' ";
        $sql=$sql. $page->limit;
        //echo $sql;
        $info = $follow->query($sql);
        //4. 获得页码列表
        $pagelist = $page->fpage();

        $this->assign('pagelist', $pagelist);//跳转导航列表


        if(!empty($info)){
            $str='';
            foreach($info as $v){
                $str=$str.$v["follower_id"].",";
            }
            $str = rtrim($str,',');

            $user=D("User");
            $uinfo=$user->where("user_id in (".$str.")")->select();
            $this->assign("uinfo",$uinfo);
        }else{
            $this->assign("follow_info","你还没有粉丝，多多分享，总会有的！");
        }
        $this->display();
    }

    //删除分享条目
    function del_message($message_id){
        $message = D("Message");
        $rst = $message->delete($message_id);
        if ($rst > 0) {
            $this->redirect('homeright');
        } else {
            $this->redirect('homeright','信息删除失败！');
        }
    }

}